package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyProtectAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.bean.ProtectListBooksBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

public class EmergencyProtectActivity extends CommonListActivity {
    private MySpinner spOrgName;
    private MySpinner spTeamType;
    private EditText etName;
    private View itemTopView;
    private OrgListBean orgListBean;
    private ImageView ivSearch;

    private List<ProtectListBooksBean> protectListBooksBeans = new ArrayList<>();
    private List<String> teamTypeList = new ArrayList<>();

    private String teamType = "";
    private String orgId = "";
    private String name = "";
    private List<String> orgNameList = new ArrayList<>();
    private List<String> orgIdList = new ArrayList<>();

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyProtectAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_PROTECT;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();

        itemTopView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_protect_top, null);
        spOrgName = itemTopView.findViewById(R.id.spOrgName);
        spOrgName.getmText().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
        spTeamType = itemTopView.findViewById(R.id.spTeamType);
        etName = itemTopView.findViewById(R.id.et_name);
        // ivSearch = itemTopView.findViewById(R.id.iv_search);
        mAdapter.addHeaderView(itemTopView);

    }

    @Override
    public void setListener() {
        super.setListener();
        teamTypeList.add("护院");
        teamTypeList.add("护站");
        teamTypeList.add("护区");
        teamTypeList.add("护路");
        teamTypeList.add("护桥");
        teamTypeList.add("护队");

        spTeamType.setmDatas(teamTypeList, new MySpinner.CallBack() {
            @Override
            public void onSelect(int postion) {
                teamType = teamTypeList.get(postion);
                spTeamType.getmText().setText(teamType);
                getData();
            }
        });

        getOrg();


//        ivSearch.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                String orgNme = spOrgName.getmText().getText().toString();
//                teamType = spTeamType.getmText().getText().toString();
//                String name = TextUtils.isEmpty(etName.getText().toString()) ? "" : etName.getText().toString();
//                for (int i = 0; i < orgNameList.size(); i++) {
//                    if (orgNme.equals(orgNameList.get(i))) {
//                        orgId = orgIdList.get(i);
//                        requestData(orgId, teamType, name);
//                        return;
//                    }
//                }
//
//
//            }
//        });

    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                orgNameList = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    orgNameList.add(listBean.getOrgName());
                    orgIdList.add(listBean.getId());
                }
                if (orgNameList != null && orgNameList.size() > 0) {
                    spOrgName.getmText().setText(orgNameList.get(0));
                }
                getData();
                spOrgName.setmDatas(orgNameList, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        getData();
                        //orgId = orgListBean.getList().get(postion).getId();
                    }
                });
            }
        });
    }

    private void getData() {
        String orgNme = spOrgName.getmText().getText().toString();
        teamType = spTeamType.getmText().getText().toString();
        String name = TextUtils.isEmpty(etName.getText().toString()) ? "" : etName.getText().toString();
        for (int i = 0; i < orgNameList.size(); i++) {
            if (orgNme.equals(orgNameList.get(i))) {
                orgId = orgIdList.get(i);
                requestData(orgId, teamType, name);
                return;
            }
        }
    }

    private void requestData(String orgId, String teamType, String name) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_PROTECT_BOOKS);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("teamType", teamType);
        map.put("name", name);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                protectListBooksBeans = GsonUtil.jsonToList(listArray.toString(), ProtectListBooksBean.class);
                mAdapter.updateItems(protectListBooksBeans);
            }
        });

    }

}
